package com.todo.delicacy.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.StringUtils;
import com.todo.delicacy.application.convert.FoodConvert;
import com.todo.delicacy.application.entity.bo.FoodInfoBO;
import com.todo.delicacy.application.entity.condition.FoodInfoConditionBO;
import com.todo.delicacy.application.service.FoodInfoService;
import com.todo.delicacy.infrastructure.entity.condition.FoodInfoCondition;
import com.todo.delicacy.infrastructure.entity.po.FoodInfoPO;
import com.todo.delicacy.infrastructure.repository.FoodInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (FoodInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:46
 */
@Slf4j
@Service("foodInfoService")
public class FoodInfoServiceImpl implements FoodInfoService {

    @Resource
    private FoodInfoRepository foodInfoRepository;

    @Override
    public Boolean insertFoodInfo(FoodInfoBO foodInfoBO) {
        if (foodInfoBO == null) {
            return false;
        }
        FoodInfoPO alreadyFoodInfoPO = foodInfoRepository.queryFoodInfoPOByRestaurantAndName(foodInfoBO.getRestaurantId(), foodInfoBO.getName());
        if (alreadyFoodInfoPO != null) {
            log.warn("FoodInfoService#insertFoodInfo 当前餐馆已经存在相同名称的菜系，保存失败 foodInfoBO：【{}】", JSONObject.toJSONString(foodInfoBO));
            throw new BizException(BizErrorCodeEnum.SUCCESS_EXIST, "当前饭店存在相同名称菜品");
        }
        FoodInfoPO foodInfoPO = FoodConvert.convertToFoodInfoPO(foodInfoBO);
        Boolean insertResult = foodInfoRepository.insertFoodInfoPO(foodInfoPO);

        return insertResult;
    }

    @Override
    public Boolean updateFoodInfo(FoodInfoBO foodInfoBO) {
        if (foodInfoBO == null || foodInfoBO.getFoodId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FoodInfoBO alreadyFoodInfoBO = queryFoodInfoById(foodInfoBO.getFoodId());
        if (alreadyFoodInfoBO == null) {
            log.warn("FoodInfoService#updateFoodInfo 更新菜品信息失败，根据主键查询数据为空 主键Id：【{}】", foodInfoBO.getFoodId());
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!alreadyFoodInfoBO.getRestaurantId().equals(foodInfoBO.getRestaurantId())) {
            log.warn("FoodInfoService#updateFoodInfo 更新菜品信息失败，不允许更新其他饭店的菜品信息 foodInfoBO:【{}】", JSONObject.toJSONString(foodInfoBO));
            throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION, "不允许更新其他饭店的菜品信息");
        }
        if (!foodInfoBO.getName().equals(alreadyFoodInfoBO.getName())) {
            FoodInfoPO alreadyFoodInfoPO = foodInfoRepository.queryFoodInfoPOByRestaurantAndName(foodInfoBO.getRestaurantId(), foodInfoBO.getName());
            if (alreadyFoodInfoPO != null) {
                log.warn("FoodInfoService#updateFoodInfo 当前餐馆已经存在相同名称的菜系，更新失败 foodInfoBO：【{}】", JSONObject.toJSONString(foodInfoBO));
                throw new BizException(BizErrorCodeEnum.SUCCESS_EXIST, "当前饭店存在相同名称菜品");
            }
        }
        FoodInfoPO foodInfoPO = FoodConvert.convertToFoodInfoPO(foodInfoBO);
        Boolean updateResult = foodInfoRepository.updateFoodInfoPOById(foodInfoPO);
        return updateResult;
    }

    @Override
    public Boolean removeFoodInfoById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FoodInfoBO foodInfoBO = queryFoodInfoById(id);
        if (foodInfoBO == null) {
            log.info("FoodInfoService#removeFoodInfoById 删除菜品信息成功，根据主键查询数据为空 默认成功 id：【{}】", id);
            return true;
        }
        Boolean removeResult = foodInfoRepository.removeFoodInfoPOById(id);
        return removeResult;
    }


    @Override
    public Boolean removeFoodInfoByRestaurantId(Long restaurantId) {
        if (restaurantId == null || restaurantId < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<FoodInfoBO> foodInfoBOS = queryFoodInfoListByRestaurantId(restaurantId);
        if (CollectionUtils.isEmpty(foodInfoBOS)) {
            log.info("FoodInfoService#removeFoodInfoByRestaurantId 删除菜品信息成功，根据主键查询数据为空 默认成功 restaurantId：【{}】", restaurantId);
            return true;
        }
        Boolean removeResult = foodInfoRepository.removeFoodInfoByRestaurantId(restaurantId);
        return removeResult;
    }

    @Override
    public FoodInfoBO queryFoodInfoById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        FoodInfoPO foodInfoPO = foodInfoRepository.queryFoodInfoPOById(id);
        return FoodConvert.convertToFoodInfoBO(foodInfoPO);
    }

    @Override
    public List<FoodInfoBO> queryFoodInfoListByRestaurantId(Long restaurantId) {
        if (restaurantId == null || restaurantId < 0) {
            return Lists.newArrayList();
        }
        List<FoodInfoPO> foodInfoPOList = foodInfoRepository.queryFoodInfoPOByRestaurantId(restaurantId);
        if (CollectionUtils.isEmpty(foodInfoPOList)) {
            log.warn("FoodInfoService#queryFoodInfoListByRestaurantId 根据饭店Id查询美食列表 数据为空 restaurantId：【{}】", restaurantId);
            return Lists.newArrayList();
        }
        return foodInfoPOList.stream().map(FoodConvert::convertToFoodInfoBO).collect(Collectors.toList());
    }

    @Override
    public PageResult<FoodInfoBO> queryFoodInfoByCondition(FoodInfoConditionBO conditionBO) {
        if (conditionBO == null) {
            return new PageResult<>();
        }
        FoodInfoCondition foodInfoCondition = FoodConvert.convertToFoodInfoCondition(conditionBO);
        PageResult<FoodInfoPO> pageResult = foodInfoRepository.queryFoodInfoByCondition(foodInfoCondition);
        if (pageResult == null) {
            return new PageResult<>();
        }
        PageResult<FoodInfoBO> result = new PageResult<>();
        result.setResults(pageResult.getResults() == null ? null : pageResult.getResults().stream().map(FoodConvert::convertToFoodInfoBO).collect(Collectors.toList()));
        result.setHasNext(pageResult.isHasNext());
        result.setTotal(pageResult.getTotal());
        result.setPageCount(pageResult.getPageCount());
        result.setPageNum(pageResult.getPageNum());
        result.setCurrentIndex(pageResult.getCurrentIndex());
        return result;
    }

    @Override
    public List<FoodInfoBO> queryFoodInfoListByName(String foodName, Long restaurantId) {
        if (StringUtils.isBlank(foodName)) {
            return Collections.emptyList();
        }
        List<FoodInfoPO> foodInfoPOList = foodInfoRepository.queryFoodInfoListByName(foodName, restaurantId);
        if (CollectionUtils.isEmpty(foodInfoPOList)) {
            log.warn("FoodInfoService#queryFoodInfoListByName 根据名称查询美食列表 数据为空 foodName：【{}】", foodName);
            return Lists.newArrayList();
        }
        return foodInfoPOList.stream().map(FoodConvert::convertToFoodInfoBO).collect(Collectors.toList());
    }
}
